package com.ywd.materials.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywd.materials.admin.common.Common;
import com.ywd.materials.admin.dto.AdminMenuDto;
import com.ywd.materials.admin.dto.query.AdminMenuQuery;
import com.ywd.materials.admin.entity.AdminMenuEntity;
import com.ywd.materials.admin.entity.AdminRoleMenuEntity;
import com.ywd.materials.admin.exception.RPage;
import com.ywd.materials.admin.mapper.AdminMenuMapper;
import com.ywd.materials.admin.utils.TreeMenuUtil;
import com.ywd.materials.admin.vo.AdminMenuTreeVo;
import com.ywd.materials.admin.vo.AdminMenuVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * YWD
 * 2025/11/12
 */
@AllArgsConstructor
@Service
public class AdminMenuServiceImpl extends ServiceImpl<AdminMenuMapper, AdminMenuEntity> {

    private AdminRoleMenuServiceImpl adminRoleMenuServiceImpl;

    public List<AdminMenuTreeVo> getMenuTree() {
        LambdaQueryWrapper<AdminMenuEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(AdminMenuEntity::getIsDeleted, 0)
                .orderByDesc(AdminMenuEntity::getSort);
        List<AdminMenuEntity> list = this.baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(list)) return null;
        List<AdminMenuTreeVo> list2 = BeanUtil.copyToList(list, AdminMenuTreeVo.class);
        return TreeMenuUtil.buildWarehouseTree(list2);
    }

    public RPage<List<AdminMenuVo>> getMenuPage(AdminMenuQuery query) {
        Page<Object> page = PageHelper.startPage(query.getPage(), query.getLimit());
        LambdaQueryWrapper<AdminMenuEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(AdminMenuEntity::getIsDeleted, 0)
                .like(ObjectUtil.isNotEmpty(query.getType()), AdminMenuEntity::getType, query.getType())
                .like(ObjectUtil.isNotEmpty(query.getName()), AdminMenuEntity::getName, query.getName())
                .orderByDesc(AdminMenuEntity::getSort);
        List<AdminMenuEntity> list = list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) return null;
        List<AdminMenuVo> list1 = BeanUtil.copyToList(list, AdminMenuVo.class);
        return RPage.ok(list1, query.getPage(), query.getLimit(), page.getTotal());
    }

    @Transactional(rollbackFor = Exception.class)
    public Boolean addMenu(AdminMenuDto dto) {
        this.baseMapper.addMenu(dto);
        //默认为管理员配置菜单
        AdminRoleMenuEntity roleMenuEntity = new AdminRoleMenuEntity();
        roleMenuEntity.setMenuId(dto.getId());
        roleMenuEntity.setRoleId(1L);
        return adminRoleMenuServiceImpl.save(roleMenuEntity);
    }

    public Boolean updateMenu(AdminMenuDto dto) {
        return updateById(BeanUtil.copyProperties(dto, AdminMenuEntity.class));
    }

    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteMenu(Long id) {
        //删除角色菜单关联
        adminRoleMenuServiceImpl.removeByMenuId(id);
        return removeById(id);
    }

    public List<Common> getMenuPartList(String name) {
        LambdaQueryWrapper<AdminMenuEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(AdminMenuEntity::getIsDeleted, 0)
                .like(ObjectUtil.isNotEmpty(name), AdminMenuEntity::getName, name)
                .orderByDesc(AdminMenuEntity::getSort);
        List<AdminMenuEntity> list = list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) return null;
        return BeanUtil.copyToList(list, Common.class);
    }

    public List<AdminMenuTreeVo> getByRoleId(Long roleId) {
        List<AdminRoleMenuEntity> list = adminRoleMenuServiceImpl.getByRoleId(roleId);
        if (CollectionUtil.isEmpty(list)) return null;
        List<Long> list1 = list.stream().map(AdminRoleMenuEntity::getMenuId).toList();
        LambdaQueryWrapper<AdminMenuEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AdminMenuEntity::getId, list1);
        List<AdminMenuEntity> list2 = list(queryWrapper);
        return BeanUtil.copyToList(list2, AdminMenuTreeVo.class);
    }

}
